package main

import (
	"context"
	"errors"

	"github.com/redis/go-redis/v9"
)

const (
	// RedisKeyShortUrl Redis Key 前缀 -短链
	RedisKeyShortUrl = "short_link:url"
)

type RedisConf struct {
	Host string
	Db   int
	Pass string
}

func redisConnect(c *RedisConf) (*redis.Client, error) {
	client := redis.NewClient(&redis.Options{
		Addr:     c.Host,
		Password: c.Pass,
		DB:       c.Db,
	})

	if err := client.Ping(context.Background()).Err(); err != nil {
		return nil, errors.New("ping redis err")
	}

	return client, nil
}

func getRdb() (*redis.Client, error) {
	conf := &RedisConf{
		Host: "172.24.154.81:6379",
		Db:   0,
		Pass: "",
	}

	return redisConnect(conf)
}

type ShortUrl struct {
	Id         int    `json:"id"`
	Type       string `json:"type"`
	Path       string `json:"path"`
	PathHash   string `json:"path_hash"`
	Domain     string `json:"domain"`
	Ticket     string `json:"ticket"`
	Other      string `json:"other"`
	Remark     string `json:"remark"`
	IsDelete   int    `json:"is_delete"`
	CreateTime int    `json:"create_time"`
	UpdateTime int    `json:"update_time"`
	ExpireTime int    `json:"expire_time"`
	MerId      int    `json:"mer_id"`
	AgentId    int64  `json:"agent_id"` // 分销商Id
}
